package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TPurchase;
import com.wit.witmg.entity.TPurchcontr;
import com.wit.witmg.mapper.TPurchaseMapper;
import com.wit.witmg.mapper.TPurchcontrMapper;
import com.wit.witmg.service.ITPurchcontrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-01-14
 */
@Service
public class TPurchcontrServiceImpl extends ServiceImpl<TPurchcontrMapper, TPurchcontr> implements ITPurchcontrService {

    @Resource
    private TPurchaseMapper tPurchaseMapper;
    @Override
    public Object add(HttpServletRequest request, String json) {
        //参数json（包含pid,stat）；根据pid联表t_purchase采购计划表，新增时，把参数xj++；修改时，同步采购计划中的stat，stat==2时把bj++。
//        json="{\"guid\":10001,\"stat\":1,\"pid\":\"13\",\"bn\":\"6F.6.7.ZnO\",\"na\":\"氧化锌\",\"size\":\"ZnO\",\"remark\":\"\",\"unit\":\"kg\",\"pqt\":\"3000\",\"indate\":\"2025-01-17 15:01:00\",\"oid\":\"14\",\"belong\":\"电阻片项目部\",\"supid\":\"4\",\"supna\":\"杭州广恒锌业有限公司\"}";
        TPurchcontr t= JSON.parseObject(json,TPurchcontr.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
            if(t.getStat()==2){
                tPurchaseMapper.upbj(t.getPid());
            }
        }else{
            t.setDate(new Date());
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
            tPurchaseMapper.upxj(t.getPid());
        }
        tPurchaseMapper.upStat(t.getPid(),t.getStat());
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, Integer id,Integer stat,Integer supid, Integer oid, Integer pid, String size, String bn) {
        //参数：oid、pid或bn或size返回数组 pid为空时返回最近100条数据；id返回单条数据。其中，bn和size是模糊查询
        QueryWrapper<TPurchcontr> qw=new QueryWrapper<>();
        if(id!=null){
            qw.eq("id",id);
            qw.last("limit 1");
            return JsonResult.success(getOne(qw));
        }
        if(stat!=null){
            qw.eq("stat",stat);
        }
        if(pid!=null){
            if(pid==0){
                qw.orderByDesc("id");
                qw.last("limit 100");
            }else {
                qw.eq("pid", pid);
            }
        }
        if(supid!=null){
            qw.eq("supid",supid);
        }
        if(oid!=null){
            qw.eq("oid",oid);
        }
        if(size!=null){
            qw.like("size",size);
        }
        if(bn!=null){
            qw.like("bn",bn);
        }
        return JsonResult.success(list(qw));
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
